package com.pinyougou.shop.controller;

import com.pinyougou.pojo.Result;
import com.pinyougou.util.FastDFSClient;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Author: Feng.Wang
 * @Company: Zelin.ShenZhen
 * @Description:
 * @Date: Create in 2019/6/11 16:36
 */
@RestController
public class UploadController {
    @Value("${FILE_SERVER_URL}")
    private String FILE_SERVER_URL;
    //进行文件上传
    @RequestMapping("/upload")
    public Result upload(MultipartFile file){
        try {
            //1.得到要上传的文件名
            String originalFilename = file.getOriginalFilename();
            //2.得到文件的后缀名
            String suffix = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
            //3.开始文件上传
            FastDFSClient fastDFSClient = new FastDFSClient("classpath:config/fdfs_client.conf");
            //4.进行文件上传
            String uploadFile = fastDFSClient.uploadFile(file.getBytes(), suffix);
            //5.拼凑文件路径
            String url = FILE_SERVER_URL +  uploadFile;
            //6.返回上传成功
            return new Result(true,url);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"文件上传失败！");
        }
    }
}
